package com.qidian.hw.servlet;

import com.qidian.hw.pojo.News;
import com.qidian.hw.service.NewsService;
import com.qidian.hw.service.impl.NewsServiceImpl;
import com.qidian.hw.tools.Const;
import com.qidian.hw.tools.FormatUtil;
import com.qidian.hw.tools.JSONTools;
import com.qidian.hw.tools.dto.Pager;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author ꒰ঌ(李ᆺ琛)໒꒱
 * @Date 2022/8/19 20:13
 * @Version 1.8
 */
public class NewsServlet extends HttpServlet {
    private final NewsService newsService = new NewsServiceImpl ();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理POST请求的中文乱码问题
        if (req.getMethod ().equalsIgnoreCase ("POST")) {
            req.setCharacterEncoding ("utf-8");
        }
        String op = req.getParameter ("op");
        switch (op) {
            // 保存数据
            case "save":
                save (req, resp);
                break;
            // 回显数据
            case "input":
                input (req, resp);
                break;
            // 修改数据
            case "update":
                update (req, resp);
                break;
            // 显示数据列表
            case "list":
                list (req, resp);
                break;
            // 删除数据
            case "delete":
                delete (req, resp);
                break;
            //批量删除
            case "deleteBatch":
                deleteBatch (req, resp);
                break;
            case "queryByPage": {
                queryByPage (req, resp);
                break;
            }
            default:
                super.service (req, resp);
        }
    }

    private void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int result = 0;
        String[] ids = req.getParameterValues ("newsId");
        if (ids != null && ids.length > 0) {
            result = newsService.deleteBatch (ids);
        }
        resp.setContentType (Const.RES_HTML);
        if (result > 0) {
            resp.getWriter ().write ("删除成功！");
        } else resp.getWriter ().write ("删除失败！");
    }

    private void queryByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int page = 1;
        try {
            page = Integer.parseInt (req.getParameter ("page"));
        } catch (NumberFormatException e) {
        }
        int size = 10;
        try {
            size = Integer.parseInt (req.getParameter ("size"));
        } catch (NumberFormatException e) {
        }
        String newsTitle = req.getParameter ("newsTitle");
        Pager pager = newsService.queryByPage (page, size, newsTitle);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (JSONTools.object2json (pager));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<News> newss = newsService.listNews ();
        String jsons = JSONTools.object2json (newss);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (jsons);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long newsId = 0L;
        try {
            newsId = Long.parseLong (req.getParameter ("newsId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        int result = newsService.deleteNews (newsId);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("删除成功！");
        } else resp.getWriter ().write ("删除失败！");
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String newsTitle = req.getParameter ("newsTitle");
        String newsDesc = req.getParameter ("newsDesc");
        String newsImg = req.getParameter ("newsImg");
        String newsKeyword = req.getParameter ("newsKeyword");
        int newsClickCount = 0;
        Long newsId = 0L;
        try {
            newsClickCount = Integer.parseInt (req.getParameter ("newsClickCount"));
            newsId = Long.parseLong (req.getParameter ("newsId"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        String newsDetails = req.getParameter ("newsDetails");
        News news = new News (newsTitle, newsDesc, newsImg, newsKeyword, newsClickCount, newsDetails);
        news.setNewsId (newsId);
        news.setModifyTime (FormatUtil.getNow ());
        int result = newsService.updateNews (news);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("更新成功！");
        } else resp.getWriter ().write ("更新失败！");
    }

    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long newsId = Long.parseLong (req.getParameter ("newsId"));
        News news = newsService.queryNewsById (newsId);
        String json = JSONTools.object2json (news);
        resp.setContentType (Const.RES_JSON);
        resp.getWriter ().write (json);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String newsTitle = req.getParameter ("newsTitle");
        String newsDesc = req.getParameter ("newsDesc");
        String newsImg = req.getParameter ("newsImg");
        String newsKeyword = req.getParameter ("newsKeyword");
        int newsClickCount = 0;
        try {
            newsClickCount = Integer.parseInt (req.getParameter ("newsClickCount"));
        } catch (NumberFormatException e) {
            e.printStackTrace ();
        }
        String newsDetails = req.getParameter ("newsDetails");
        News news = new News (newsTitle, newsDesc, newsImg, newsKeyword, newsClickCount, newsDetails);
        int result = newsService.saveNews (news);
        resp.setContentType (Const.RES_HTML);
        if (result == 1) {
            resp.getWriter ().write ("添加成功！");
        } else resp.getWriter ().write ("添加失败！");
    }

}
